const Router = require("@koa/router");
const jwt = require("jsonwebtoken");
const bcrypt = require("bcrypt");
const router = new Router();

router.get("/pulluserlist", async(ctx, next) => {
    try {
        // 检查请求头部中是否包含 token
        const token = ctx.header.authorization;
        if (!token) {
            ctx.status = 401; // 未授权
            ctx.body = { success: false, error: "未提供身份验证信息，请登录账户。" };
            return;
        }

        // 如果请求头部中包含 token，则继续处理请求
        const page = parseInt(ctx.query.page) || 1; // 获取页码，默认为第一页
        const pageSize = 10; // 每页结果数量
        const offset = (page - 1) * pageSize;

        // 查询用户列表
        const userList = await ctx.state.Customer.findAll({
            offset,
            limit: pageSize,
        });

        // 获取用户总数
        const totalCount = await ctx.state.Customer.count();

        ctx.status = 200;
        ctx.body = {
            success: true,
            totalCount,
            resultCount: userList.length,
            userList,
        };
    } catch (error) {
        console.error("Error fetching user list:", error);
        ctx.status = 500;
        ctx.body = { success: false, error: "获取用户列表时发生错误。" };
    }
});

module.exports = router;